<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <meta name="xbsj-labels" content="Earth示例">
    </meta>
    <title>效果-阴影</title>
    <!-- 0 引入js文件：XbsjEarth.js和vue.min.js -->
    <script src="../../XbsjEarth/XbsjEarth.js"></script>
    <script src="./scripts/vue.min.js"></script>
    <style>
        html,
        body {
            width: 100%;
            height: 100%;
            margin: 0px;
            padding: 0px;
        }

        input {
            width: 350px;
        }

        .checkbox {
            display: inline-block;
            cursor: pointer;
        }

        .shadowbox {
            width: 20px;
            height: 16px;
        }

        .defultbtn {
            display: inline-block;
            text-align: center;
            min-width: 60px;
            height: 38px;
            padding: 0 10px;
            line-height: 38px;
            border-radius: 100px;
            border: 1px solid #C9C9C9;
            background-color: #FFF;
            color: #555;
            cursor: pointer;
            margin-bottom: 4px;
        }

        .defultbtn:hover {
            background-color: #b3daf8;
        }

        .btnon {
            background-color: #1E9FFF;
            color: #fff;
            border: 1px solid #1E9FFF;
        }
    </style>
</head>

<body>
    <div id="vueApp" style="width: 100%; height: 100%; background: grey; position: relative;">
        <earth-comp></earth-comp>
    </div>

    <script>
        // 1 创建Earth的vue组件
        var EarthComp = {
            template: `
                <div style="width: 100%; height: 100%">
                    <div ref="earthContainer" style="width: 100%; height: 100%">
                    </div>
                    <div class="box" style="position: absolute; left: 18px; top: 18px; color: white; background: rgba(0, 0, 0, 0.6); padding: 20px; border-radius: 25px;min-width:350px; font-size:24px; font-family: 宋体;">
                        <div class="defultbtn" :class="{'btnon':enabled}" @click="enabled = !enabled">阴影</div><br/>
                        <span>暗度：{{darkness}}</span><br/><input type="range" min="0" step="0.01" max="1" v-model.number="darkness"><br/>
                        <span>最大距离：{{maximumDistance}}</span><br/><input type="range" min="1" step="1" max="100000" v-model.number="maximumDistance"><br/>
                        <span @click="terrainShadow = !terrainShadow" class="checkbox"><input class="shadowbox" type="checkbox" v-model="terrainShadow">地形阴影</span><br/>
                        <span @click="softShadows = !softShadows" class="checkbox"><input class="shadowbox" type="checkbox" v-model="softShadows">软阴影</span><br/>
                        <span @click="normalOffset = !normalOffset" class="checkbox"><input class="shadowbox" type="checkbox" v-model="normalOffset">法向量偏移</span><br/>
                    </div>
                </div>
            `,
            data() {
                return {
                    _earth: undefined, // 注意：Earth和Cesium的相关变量放在vue中，必须使用下划线作为前缀！
                    enabled: true,
                    darkness: 0,
                    maximumDistance: 0,
                    terrainShadow: true,
                    softShadows: false,
                    normalOffset: true
                };
            },
            // 1.1 资源创建
            mounted() {
                // 1.1.1 创建地球
                var earth = new XE.Earth(this.$refs.earthContainer);

                this._earth = earth;

                // 1.1.2 场景配置
                // 默认显示天地图影像
                earth.sceneTree.root = {
                    "children": [{
                        "ref": 'tileset',
                        "czmObject": {
                            "name": '大雁塔',
                            "xbsjType": "Tileset",
                            "url": '../assets/dayanta/tileset.json',
                            "xbsjUseOriginTransform": false,
                            "skipLevelOfDetail": false
                        },
                    }, {
                        "czmObject": {
                            "name": '离线影像',
                            "xbsjType": "Imagery",
                            "xbsjImageryProvider": XE.Obj.Imagery.defaultImageryProviderConfig,
                        }
                    }
                    ]
                }

                // 设置相机位置
                // earth.camera.position.toString()和earth.camera.toAllJSONStr()这两个方法可获取相机位置
                earth.camera.position = [1.901703278961517, 0.5971775186512027, 804.7994518114391];
                earth.camera.rotation = [1.5898393712632242e-13, -0.7855880281448457, 1.0658141036401503e-14];

                // 1.1.3 数据绑定
                this._enabledUnbind = XE.MVVM.bind(this, 'enabled', earth.effect.shadow, 'enabled');
                this._darknessUnbind = XE.MVVM.bind(this, 'darkness', earth.effect.shadow, 'darkness');
                this._maximumDistanceUnbind = XE.MVVM.bind(this, 'maximumDistance', earth.effect.shadow, 'maximumDistance');
                this._terrainShadowUnbind = XE.MVVM.bind(this, 'terrainShadow', earth.effect.shadow, 'terrainShadow');
                this._softShadowsUnbind = XE.MVVM.bind(this, 'softShadows', earth.effect.shadow, 'softShadows');
                this._normalOffsetUnbind = XE.MVVM.bind(this, 'normalOffset', earth.effect.shadow, 'normalOffset');

                // 1.1.4 设置初始值
                earth.effect.shadow.enabled = true;

                // only for Debug
                window.earth = earth;
            },
            methods: {
            },
            // 1.2 资源销毁
            beforeDestroy() {
                // vue程序销毁时，需要清理相关资源
                this._enabledUnbind = this._enabledUnbind && this._enabledUnbind();
                this._darknessUnbind = this._darknessUnbind && this._darknessUnbind();
                this._maximumDistanceUnbind = this._maximumDistanceUnbind && this._maximumDistanceUnbind();
                this._maximumDistanceUnbind = this._maximumDistanceUnbind && this._maximumDistanceUnbind();
                this._terrainShadowUnbind = this._terrainShadowUnbind && this._terrainShadowUnbind();
                this._softShadowsUnbind = this._softShadowsUnbind && this._softShadowsUnbind();
                this._normalOffsetUnbind = this._normalOffsetUnbind && this._normalOffsetUnbind();
                this._earth = this._earth && this._earth.destroy();
            },
        }

        // 2 创建vue程序
        // XE.ready()用来加载Cesium.js等相关资源
        XE.ready().then(() => {
            var app = new Vue({
                el: '#vueApp',
                components: {
                    EarthComp,
                },
            });
        });
    </script>
</body>

</html>